#! /bin/bash

# 获取环境变量
PLAY_HOME=/opt/playground

function get_version() {
    # 简化版本检查，只要是datax.tar.gz就可以
    if [[ $1 == "datax.tar.gz" ]]; then
        version="current"
        echo "软件包名：$1"
    else
        echo "未找到正确的软件包，期望的软件包名为：datax.tar.gz"
        exit 1
    fi
}

function configureDataXPython() {
    datax_home=$1
    
    # 修改datax.py中的DATAX_HOME路径
    sed -i "s#DATAX_HOME = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#DATAX_HOME = \"${datax_home}\"#g" \
        ${datax_home}/bin/datax.py
}

function configureDataXJvm() {
    datax_home=$1
    
    # 配置JVM参数
    cat > ${datax_home}/conf/datax.properties << EOF
# DataX运行需要的JVM参数
datax.jvm=-Xms1G -Xmx1G -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${datax_home}/log
# DataX超时时间
datax.timeoutMinutes=1440
EOF
}

function installDataX() {
    # 在frames.txt中查看是否需要安装datax
    dataxInfo=`egrep "^datax" $PLAY_HOME/frames.txt`
    
    datax=`echo $dataxInfo | cut -d " " -f1`
    isInstall=`echo $dataxInfo | cut -d " " -f2`
    dataxNodes=`echo $dataxInfo | cut -d " " -f3`
    node=`hostname`
    
    get_version $datax
    version=$version
    
    echo $datax
    echo $isInstall
    
    # 是否安装
    if [[ $isInstall = "true" && $dataxNodes =~ $node ]]; then
        # 查看$PLAY_HOME/frames目录下是否有datax安装包
        dataxIsExists=`find $PLAY_HOME/frames -name $datax`
        echo $dataxIsExists
        
        if [[ ${#dataxIsExists} -ne 0 ]]; then
            if [[ ! -d /opt/app ]]; then
                mkdir /opt/app && chmod -R 775 /opt/app
            fi
            
            # 删除旧的
            datax_home_old=`find /opt/app -maxdepth 1 -name "datax*"`
            for i in $datax_home_old; do
                rm -rf $i
            done
            
            # 解压到指定文件夹/opt/app中
            echo "开始解压DataX安装包"
            tar -zxvf $dataxIsExists -C /opt/app >& /dev/null
            echo "DataX安装包解压完毕"
            
            datax_home=`find /opt/app -maxdepth 1 -name "datax*"`
            
            # 创建日志目录
            if [[ ! -d $datax_home/log ]]; then
                mkdir $datax_home/log
            fi
            
            chmod -R 775 $datax_home
            
            # 配置DataX的Python脚本
            configureDataXPython $datax_home
            
            # 配置DataX的JVM参数
            configureDataXJvm $datax_home
            
            # 配置环境变量
            profile=/etc/profile
            sed -i "/^export DATAX_HOME/d" $profile
            echo "export DATAX_HOME=$datax_home" >> $profile
            sed -i "/^export PATH=\$PATH:\$DATAX_HOME\/bin/d" $profile
            echo "export PATH=\$PATH:\$DATAX_HOME/bin" >> $profile
            
            # 更新/etc/profile文件
            source /etc/profile && source /etc/profile
            
            echo "--------------------"
            echo "|  DataX安装成功！ |"
            echo "--------------------"
        else
            echo "$PLAY_HOME/frames目录下没有DataX安装包"
        fi
    else
        echo "DataX不允许安装在当前节点"
    fi
}

installDataX 